package com.gxkj.projects.myshopx.dao;

import com.gxkj.common.hibernate.HibernateBaseRepositoryImpl;
import com.gxkj.common.utils.ListPager;
import com.gxkj.projects.myshopx.entitys.Suppliers;
import com.gxkj.projects.myshopx.enums.SuppliersStates;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
public class SuppliersDaoImpl extends HibernateBaseRepositoryImpl {

    public ListPager<Suppliers> doPageHQL(int pagenNo, int pageSize, Suppliers entity){
        ListPager<Suppliers> pager = new ListPager<Suppliers>();
        pager.setPageNo(pagenNo);
        pager.setRowsPerPage(pageSize);
        String hql  = " from Suppliers a ";
        Map<String,Object> param = new HashMap<String,Object>();
        if(entity==null || entity.getStates() == null   ){
            hql += " where states != :states ";
            param.put("states", SuppliersStates.DEL);

        }else {
            hql += " where states = :states ";
            param.put("states",entity.getStates());
        }
        if(entity != null){

            if(StringUtils.isNotBlank(entity.getName())){
                hql += " and name like :name ";
                param.put("name","%"+entity.getName()+"%");
            }
        }
        return  this.selectPageByHql(hql,param,pager);
    }
    public boolean isExitByName(String id,String name){
        String hql = null;
        Map<String,Object> param = new HashMap<String,Object>();
        if(StringUtils.isBlank(id)){
            hql = " from Suppliers a where states != :states and name=:name ";
            param.put("states", SuppliersStates.DEL);
            param.put("name",name);
        }else {
            hql = " from Suppliers a where states != :states and name=:name and id !=:id ";
            param.put("states", SuppliersStates.DEL);
            param.put("name",name);
            param.put("id",id);
        }
        Suppliers entity = this.selectFirstOneByHQL(hql,param);
        if(entity == null){
            return false;
        }else {
            return true;
        }

    }
}
